# _*_ coding utf-8 _*_
# FILENAME：.py
# DESCRIPTION: 
# AUTH: 
# DATE: 2022/6/110:56 上午

from PyQt5.QtWidgets import *

from components.MessageBoxComponent import MessageBox
from components.SelectFactoryComponent import SelectFactoryComponent
from models.baseModel import get_session
from models.baseModel import *
from models.inventoryModel import InventoryModel

class VersionDialog(QDialog):

    __id = 0  # 版本编号
    '''
    id 版本编号
    '''
    def __init__(self, parent=None, id=0):
        super(VersionDialog, self).__init__(parent)
        self.setFixedSize(400, 200)
        self.init_ui(parent)
        self.__id = id
        title = "新增版本"
        if id > 0:
            title = "编辑版本"
            self.edit_data()
        self.setWindowTitle(title)

    #  编辑-赋值
    def edit_data(self):
        session = get_session()
        data = session.query(
            InventoryModel,
        ).filter(
            InventoryModel.id == self.__id
        ).all()
        session.close()
        if data:
            self.ver_edit.setText(data[0].version_name)
            self.fac_combox.selectedById(int(data[0].factory_id))

    def init_ui(self, parent):
        '''水平布局'''
        hbox = QHBoxLayout()
        self.save_btn = QPushButton()
        self.save_btn.setText('保存')
        self.save_btn.setFixedWidth(80)
        self.save_btn.clicked.connect(self.save_btn_click)

        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.cancel_btn.setFixedWidth(80)
        self.cancel_btn.clicked.connect(self.cancel_btn_click)

        hbox.addStretch(1)  # 占位
        hbox.addWidget(self.save_btn)
        hbox.addWidget(self.cancel_btn)
        hbox.addStretch(1)  # 占位
        btnPanelWidget = QWidget()
        btnPanelWidget.setLayout(hbox)

        '''表单布局'''
        self.fac_lab = QLabel()
        self.fac_lab.setText('工厂：')
        self.fac_combox = SelectFactoryComponent()
        self.fac_combox.setObjectName("fac_combox")
        self.fac_combox.setPlaceholderText('请选择工厂')
        self.fac_combox.setCurrentIndex(0)

        self.ver_lab = QLabel()
        self.ver_lab.setText('版本：')
        self.ver_edit = QLineEdit()
        self.ver_edit.setObjectName("ver_edit")
        self.ver_edit.setPlaceholderText('请输入版本')

        gbox = QGridLayout()
        gbox.addWidget(self.fac_lab, 0, 0, 1, 1)
        gbox.addWidget(self.fac_combox, 0, 1, 1, 1)

        gbox.addWidget(self.ver_lab, 1, 0, 1, 1)
        gbox.addWidget(self.ver_edit, 1, 1, 1, 1)

        gbox.addWidget(btnPanelWidget, 3, 0, 1, 2)

        vbox = QVBoxLayout()
        vbox.addLayout(gbox)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

    def save_btn_click(self, parent):
        if self.ver_edit.text().strip() == '':
            messageBox = MessageBox()
            messageBox.warrning(title="提醒", msg="版本不能为空")
            return

        # 获取工厂编号
        factory_id = self.fac_combox.getId()
        # 版本
        version_name = self.ver_edit.text()
        #
        model = InventoryModel()
        session = get_session()
        time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        if self.__id > 0:
            # 编辑
            model.id = self.__id
            # 赋值
            params = {
                'version_name': self.ver_edit.text(),
                'factory_id': self.fac_combox.getId(),
                'update_date': time
            }
            # 更新
            session.query(InventoryModel).filter(InventoryModel.id == model.id).update(params)
            # 提交&关闭
            session.commit()
            session.close()
            # 刷新列表
            self.parent().query_click()
        else:
            # 新增
            model.version_name = version_name
            model.factory_id = factory_id
            model.create_date = time
            session.add(model)
            session.commit()
            session.close()
            # 刷新列表
            self.parent().query_click()
        #
        self.close()

    def cancel_btn_click(self):
        self.close()

    @staticmethod
    def get_dialog(parent=None, id=0):
        dialog = VersionDialog(parent, id)
        return dialog.exec()

